/**
 * Created by Myron on 2016/2/14.
 */
var async = require('async');
var config = require('../../../config.js'); //必要
var wxjsapi = require('wx_jsapi_sign'); //微信分享必要
var fs = require('fs');
var MyronWX=require('../../../lib/MyronWx.js');

var _VIEW_PATH = '../app_module/hongbao/views/';


module.exports = {
    //分享页面测试代码
    index: function (req, res) {
        var url = 'http://'+config.domain+'/extra'+req.url;
        console.log(url);
        var wxconfig={
            appId: config.wx.app_id,
            appSecret: config.wx.app_secret,
            cache_json_file:config.wx.cache_json_file
        };
        var openid=req.AV.user.get('openid');
        MyronWX.getUserInfo(wxconfig,openid).then(function(UserInfo){
            if(UserInfo.subscribe === 0){
                console.log('没有关注，请先关注果然100');
                res.render(_VIEW_PATH+'huodong/err',{appId:'',timestamp:'',nonceStr:'',signature:'',url:url});
            }else{
                wxjsapi.getSignature(wxconfig)(url, function(error, result) {
                    if (error) {
                        res.json({
                            'error': error
                        });
                    } else {
                        console.log(result.signature);
                        res.render(_VIEW_PATH+'huodong/index',{appId:result.appId,timestamp:result.timestamp,nonceStr:result.nonceStr,signature:result.signature,url:url});
                        //res.json(result);
                    }
                });
            }
        }).catch(function(){
            res.render(_VIEW_PATH+'huodong/err');
            return ;
        });

    },
    test:function(req, res){
        var url = 'http://'+config.domain+'/extra'+req.url;
        res.render(_VIEW_PATH+'huodong/index',{appId:'',timestamp:'',nonceStr:'',signature:'',url:url});

    }
}